package com.example.minzhengtong.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.example.minzhengtong.Const
import com.example.minzhengtong.HttpRequest
import com.example.minzhengtong.R
import com.example.minzhengtong.base.BaseFg
import com.example.minzhengtong.data.UserBean
import com.example.minzhengtong.databinding.FragmentMineBinding
import com.example.minzhengtong.utils.SpUtil
import com.gyf.immersionbar.ImmersionBar

/**
 * created by WY on 2021/5/27.
 */
class MineFG : BaseFg() {

    companion object {
        @JvmStatic
        fun newInstance(param1: String = "", param2: String = "") =
            MineFG().apply {
                arguments = Bundle().apply {
                }
            }
    }

    private lateinit var binding : FragmentMineBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentMineBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
        ImmersionBar.with(this).titleBar(binding.setting).statusBarDarkFont(false).init()

        setData()
        binding.setting.setOnClickListener {
            SettingA.enterThis(baseContext)
        }

    }

    private fun setData(){
        if (SpUtil.getInstance().existKey(Const.USER_BEAN)){
            try {
                val bean = SpUtil.getInstance().getObject(Const.USER_BEAN) as UserBean
                bean.apply {
                    baseContext.runOnUiThread {
                        Glide.with(this@MineFG).load(data.wxtx).error(R.mipmap.toux).placeholder(R.mipmap.toux).into(binding.head)
                        binding.name.text = "*${data.uname.substring(1)}"
                    }
                }
            }catch (e : Exception){
                e.printStackTrace()
                SpUtil.getInstance().deleteData(Const.USER_BEAN)
                HttpRequest().detail(this)
            }
        }
    }

    override fun initListener() {
        binding.gjj.setOnClickListener {
            FiveChooseA.enterThis(baseContext)
        }
        binding.personal.setOnClickListener {
            PersonalA.enterThis(baseContext)
        }
    }

    override fun requestSuccess(json: String) {
        super.requestSuccess(json)
        setData()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        HttpRequest().detail(this)
    }

    override fun lazyLoad() {

    }
}